package com.example.timetable.fragment;

import static android.content.Context.MODE_PRIVATE;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.timetable.adapter.CourseAdapter;
import com.example.timetable.dao.Course;
import com.example.timetable.databinding.FragmentPlusBinding;
import com.example.timetable.databinding.PopupItemBinding;
import com.example.timetable.dialog.AddCourseDialog;
import com.example.timetable.dialog.DateEditDialog;

import org.litepal.LitePal;

import java.util.List;

/**
 * 添加课程用的碎片
 */
public class PlusFragment extends Fragment {


    private FragmentPlusBinding binding;
    private PopupWindow window = null;
    private List<Course> courseList;
    private CourseAdapter courseAdapter;


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        binding = FragmentPlusBinding.inflate(inflater, container, false);
        binding.more.setOnClickListener(v -> {
            showWindow(binding.more);
        });
        show();
        return binding.getRoot();
    }

    /**
     * 所有课程
     */
    private void show() {
        courseList = LitePal.findAll(Course.class);
        if (courseAdapter == null) {
            courseAdapter = new CourseAdapter(getContext(),courseList);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
            binding.rvCourse.setLayoutManager(layoutManager);
            binding.rvCourse.setAdapter(courseAdapter);
        } else {
            Toast.makeText(getContext(), "暂无数据，请添加课程！", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     *  添加课程和修改开学日期的弹框
     */
    private void showWindow(View view) {
        //获取要弹出的菜单
        PopupItemBinding binding = PopupItemBinding.inflate(getLayoutInflater());
        binding.add.setOnClickListener(v -> {
            AddCourseDialog.addCourse(getActivity(), save -> {
                if (save) {
                    Toast.makeText(getContext(), "添加课程成功！", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getContext(), "添加失败！", Toast.LENGTH_SHORT).show();
                }
            });
            window.dismiss();
            window = null;
        });
        binding.upDate.setOnClickListener(v -> {
//            ShareManager.shareText(getContext(),"测试测试");
            DateEditDialog.alterDate(getContext(),getContext().getSharedPreferences("config", MODE_PRIVATE));
            window.dismiss();
            window = null;
        });
        if (window == null) {
            //创建PopupWindow,第一个参数是将要展示的布局内容，第二、三个分别对应宽和高
            window = new PopupWindow(binding.getRoot(), LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT);
            window.setTouchable(true);
            window.setOutsideTouchable(true);
            window.setFocusable(true);
            window.update();
        }
        // 设置PopupWindow弹出的位置，第一个参数是PopupWindow依附的View,
        // 第二个是坐标偏移量相对的起始点
        // 最后两个是偏移量
//        window.showAtLocation(view, Gravity.END | Gravity.TOP, 0, 0);
        window.showAsDropDown(view);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}